// Copyright 2007 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package lichen.pages.topic;

import java.util.List;

import lichen.components.Layout;
import lichen.entities.forum.Forum;
import lichen.entities.post.Post;
import lichen.entities.post.Topic;
import lichen.entities.user.User;
import lichen.internal.services.MenuImpl;
import lichen.pages.Operations;
import lichen.pages.base.BasePage;
import lichen.pages.base.LogInfoPage;
import lichen.pages.base.MenuPage;
import lichen.pages.forum.ViewForum;
import lichen.services.Menu;
import lichen.services.PostService;

import org.apache.tapestry.annotations.Component;
import org.apache.tapestry.annotations.Inject;
import org.apache.tapestry.annotations.InjectPage;

/**
 * 编辑/增加一个主题
 * 
 * @author <a href="mailto:jun.tsai@gmail.com">Jun Tsai</a>
 * @version $Revision: 147 $
 * @since 0.0.1
 */
public class FormTopic extends BasePage implements LogInfoPage,MenuPage{

	/** 当前操作的实体对象 **/
	private Topic topic;

	/** 当前浏览的论坛页面 **/
	@InjectPage
	private ViewForum forumPage;

	/** 请求的响应 **/
	private String action;


	/** PostServiceImpl **/
	@Inject
	private PostService postService;

	@Component
	private Layout _layout;
	
	/** 当前浏览的论坛 **/
	private Forum forum;

	/**
	 * 当页面激活的时候.
	 * @param action 当前请求的动作.
	 * @param forum 论坛对象.
	 */
	void onActivate(String action, Forum forum) {
		this.action = action;
		this.forum = forum;
		if (Operations.NEW.equals(action)) { //新建
			this.topic = new Topic();
		}
	}
	/**
	 * 返回固化的对象
	 * @return 固化对象.
	 */
	Object [] onPassivate() {
		return new Object[] { action, this.forum };
	}

	/**
	 * 得到当前的主题对象.
	 * @return 当前操作的主题对象.
	 */
	public Post getTopic() {
		return this.topic;
	}

	/**
	 * 保存当前实体对象.
	 * @return 论坛页面
	 */
	public ViewForum onActionFromTopicForm() {
		if (Operations.NEW.equals(this.action)) {
			this.topic.setForum(this.forum);
			User user = this._layout.getUser();
			user.setTopicCount(user.getTopicCount()+1);
			postService.saveTopic(this.topic, user);
		} else {
			this.getEntityService().saveOrUpdate(this.topic);
		}
		this.forumPage.setForum(forum);
		return this.forumPage;
	}
	/**
	 * 
	 * @see lichen.pages.base.LogInfoPage#getLogInfo()
	 */
	public String getLogInfo() {
		return getMessages().get("topic.new");
	}
	/**
	 * 
	 * @see lichen.pages.base.MenuPage#contributeMenus()
	 */
	public List<Menu> contributeMenus() {
		List<Menu> menus = this.getMenuService().generateForumMenu(forum, topic);
		StringBuffer action=new StringBuffer();
		if (Operations.NEW.equals(this.action)) {
			action.append(this.getMessages().get("topic.new"));
		}else{
			action.append(this.getMessages().format("topic.edit",topic.getTitle()));
		}
		MenuImpl menu=new MenuImpl();
		menu.setDisabled(true);
		menu.setPageText(action.toString());
		
		menus.add(menu);
		return menus;
	}
}
